#region License
/* Copyright (c) 2011 Maksymilian Mulawa
	
Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products 
 * derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.Xml.XPath;

namespace Weather.com.Client.Exceptions
{
    internal class WeatherServiceErrorHandler
    {
        internal const int UNKNOWN_ERROR_CODE = 0;
        internal const int NO_LOCATION_PROVIDED_ERROR_CODE = 1;
        internal const int INVALID_LOCATION_ERROR_CODE = 2;
        internal const int INVALID_PARTNER_ID_ERROR_CODE = 100;
        internal const int INVALID_PRODUCT_CODE_ERROR_CODE = 101;
        internal const int INVALID_LICENSE_KEY_ERROR_CODE = 102;

        public void ThrowExceptionIfErrorReturned(XPathNavigator navigator)
        {
            XPathNavigator errorNode = navigator.SelectSingleNode("/error/err");
            if (errorNode != null)
            {
                int errorCode;
                string errorCodeValue = errorNode.GetAttribute("type", string.Empty);
                bool errorCodeIsInteger = int.TryParse(errorCodeValue, out errorCode);
                string errorMessage = errorNode.Value;

                if (errorCodeIsInteger)
                {
                    switch (errorCode)
                    {
                        case UNKNOWN_ERROR_CODE:
                            //An Unknown Error has occurred
                            throw new UnknownWeatherServiceException(errorMessage, errorCode);
                        case NO_LOCATION_PROVIDED_ERROR_CODE:
                            //No Location Provided
                            throw new NoLocationProvidedWeatherServiceException(errorMessage, errorCode);
                        case INVALID_LOCATION_ERROR_CODE:
                            //Invalid Location
                            throw new InvalidLocationWeatherServiceException(errorMessage, errorCode);
                        case INVALID_PARTNER_ID_ERROR_CODE:
                            //Invalid Partner Id
                            throw new InvalidPartnerIdWeatherServiceException(errorMessage, errorCode);
                        case INVALID_PRODUCT_CODE_ERROR_CODE:
                            //Invalid Product Code
                            throw new InvalidProductCodeWeatherServiceException(errorMessage, errorCode);
                        case INVALID_LICENSE_KEY_ERROR_CODE:
                            //Invalid License Key
                            throw new InvalidLicenseKeyWeatherServiceException(errorMessage, errorCode);
                        default:
                            throw new WeatherServiceErrorException(errorMessage, errorCodeValue);    
                    }
                }
                else
                {
                    throw new WeatherServiceErrorException(errorMessage, errorCodeValue);    
                }
            }
        }
    }
}